package com.lcq.gulimall.product.service.impl;

import com.lcq.gulimall.product.dao.BrandDao;
import com.lcq.gulimall.product.dao.CategoryDao;
import com.lcq.gulimall.product.entity.BrandEntity;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.Query;

import com.lcq.gulimall.product.dao.CategoryBrandRelationDao;
import com.lcq.gulimall.product.entity.CategoryBrandRelationEntity;
import com.lcq.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    BrandDao brandDao;
    @Autowired
    CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List queryList(String brandId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId);
        return this.getBaseMapper().selectList(wrapper);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catId = categoryBrandRelation.getCatelogId();
        categoryBrandRelation.setBrandName(brandDao.selectById(brandId).getName());
        categoryBrandRelation.setCatelogName(categoryDao.selectById(catId).getName());
        this.getBaseMapper().insert(categoryBrandRelation);
    }

    @Override
    public void updateBrandName(BrandEntity brand) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandName(brand.getName());
        this.getBaseMapper().update(entity,new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brand.getBrandId()));
    }

    @Override
    public List<CategoryBrandRelationEntity> getBrandsList(Long catId) {
        return this.getBaseMapper().selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
    }


}